package com.test.springboot_test.test01_mybatisplus.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.springboot_test.test01_mybatisplus.domain.Employee;
import com.test.springboot_test.test01_mybatisplus.mapper.EmployeeMapper;
import com.test.springboot_test.test01_mybatisplus.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Override
    public Employee selectById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    @Override
    public List<Employee> selectList(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        List<Employee> list = employeeMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Employee> selectListByPassword(String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("password", password);
        List<Employee> list = employeeMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Employee> selectListByName(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name", name);
        List<Employee> list = employeeMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Employee> selectListByIds() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        List<Employee> employees = employeeMapper.selectBatchIds(list);
        return employees;
    }

    @Override
    public String deleteBatch() {
        List<Integer> list = Arrays.asList(5, 6, 7, 8);
        employeeMapper.deleteBatchIds(list);
        return "ok";
    }

    @Override
    public String insertBatch() {
        ArrayList<Employee> list = new ArrayList<>();
        Employee employee1 = new Employee();
        //employee1.setId(0);
        employee1.setEmail("345@qq.com");
        employee1.setName("测试员1号");
        employee1.setPassword(123456);
        Employee employee2 = new Employee();
        //employee1.setId(0);
        employee2.setEmail("345@qq.com");
        employee2.setName("测试员1号");
        employee2.setPassword(123456);
        list.add(employee1);
        list.add(employee2);
        employeeMapper.insert(list);
        return "ok";
    }

    @Override
    public String updateEmplpoyee(Employee employee) {
        
       //employeeMapper.update
        return "ok";
    }




    @Override
    public void pageSelect() {
        /**这种状态下的分页查询，就相当于selectALL,也就是查询全部
         * 当前页码值：1
         * 每页显示数：2
         * 一共多少页：0
         * 一共多少条数据：0
         * 查询数据是有错误的
         */
        //他的sql语句其实是：select * from employee limit 1,2,但是这里只是前半部分用到了
        //所以我们需要mybatis的拦截器
        IPage page = new Page(1,2);
        employeeMapper.selectPage(page, null);
        System.out.println("当前页码值：" + page.getCurrent());
        System.out.println("每页显示数：" + page.getSize());
        System.out.println("一共多少页：" + page.getPages());
        System.out.println("一共多少条数据：" + page.getTotal());
        System.out.println("数据：" + page.getRecords());
    }


    @Override
    public void update() {
        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("王一");
        employeeMapper.updateById(employee);
    }

    @Override
    public List<Employee> list() {
        List<Employee> employee = employeeMapper.selectList(null);
        return employee;
    }

    @Override
    public void add() {
        Employee employee = new Employee();
        //employee.setId(4);
        employee.setName("王五");
        employee.setPassword(123123);
        employee.setEmail("123@qq.com");
        employeeMapper.insert(employee);
    }

    @Override
    public void delete(Integer id) {
        employeeMapper.deleteById(id);
        //employeeMapper.delete
    }

    @Autowired
    private EmployeeMapper employeeMapper;
}
